package com.tgy.JUC.StreamTest;

import javax.swing.text.html.Option;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/***
 * @ClassName: OptionalTest
 * @Description:
 * @Auther: 送你一块二向箔
 * @Date: 17:59 2022/3/14
 * @version : V1.0
 */
public class OptionalTest {
    public static void main(String[] args) {
        run1();
    }

    public static void run1() {
        Student s1 = new Student("张三", 19, 80);
        Student s2 = new Student("李四", 19, 50);
        Student s3 = new Student("王五", 23, null);
        Student s4 = new Student("赵六", 16, 90);
        Student s5 = new Student("钱七", 18, 99);
        Student s6 = new Student("孙八", 20, 40);
        Student s7 = new Student("吴九", 21, 88);
        List<Student> students = Arrays.asList(s1, s2, s3, s4, s5, s6, s7);

        for (Student student : students) {
            Optional<Student> studentOptional = Optional.of(student);
            Integer score = studentOptional.filter(s -> s.getAge() >= 18).map(Student::getScore).orElse(0);

            if (score > 80) {
                System.out.println("入选：" + student.getName());
            }
        }
    }

    public static void run2(){
        Student s1 = new Student("张三", 19, 80);
        Student s2 = new Student("李四", 19, 50);
        Student s3 = new Student("王五", 23, null);
        Student s4 = new Student("赵六", 16, 90);
        Student s5 = new Student("钱七", 18, 99);
        Student s6 = new Student("孙八", 20, 40);
        Student s7 = new Student("吴九", 21, 88);
        List<Student> students = Arrays.asList(s1, s2, s3, s4, s5, s6, s7);

        for (Student student : students) {
            if (student != null) {
                if (student.getAge() >= 18) {
                    Integer score = student.getScore();
                    if (score != null && score > 80) {
                        System.out.println("入选：" + student.getName());
                    }
                }
            }
        }
    }
}
